import os
import sys
import json

#script = os.path.abspath(__file__)
#scriptDir = os.path.abspath(os.path.dirname(__file__))
#print(script)
#print(scriptDir)
#print(os.listdir(scriptDir))

scriptDir = os.getcwd()
print(scriptDir)
print(os.listdir(scriptDir))
sys.path.append(os.path.join(scriptDir, 'orm'))
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'orm.settings')
#${scriptDir}/queryTblOssNode.py
#${scriptDir}/orm/
#├── manage.py
#└── orm/
#    └──settings.py
#└── tbase/
#    └──models.py


import django
from django.conf import settings
from django.db import models,connection

settings.configure(
    DATABASES={
        'default': {
               'ENGINE': 'django.db.backends.postgresql',
               'NAME': 'postgres',
               'USER': 'postgres',
               'PASSWORD': 'Pgxz@TbaseV2',
               'HOST': '10.23.1.195',
               'PORT': '54321'
        }
    }
)


django.setup()

with connection.cursor() as cursor:
    cursor.execute("select * from tbl_oss_node;")
    tables=cursor.fetchall()

#for table in tables:
#    print(table)

from tbase.models import TblOssNode


#for record in list(queryset):
#    print(json.dumps(record, ensure_ascii=False, indent=2))

def main(host, port, database, user, password):
    queryset = TblOssNode.objects.all().values(
        'cluster_name','node_id','node_name','node_type',
        'node_ip', 'role','slave_num','master_nid','stream_mid','server_id',
        'bin_dir','data_dir','log_dir'
    )
    for record in list(queryset):
        print(json.dumps(record))
    #return json.dumps(list(queryset), ensure_ascii=False, indent=2)


if __name__ == "__main__":
    main(*sys.argv[1:])
